package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.DTO.AddOrUpdateFenceHouseDTO;
import com.briup.VO.FenceHouseSelectByIdVO;
import com.briup.enums.CodeAndMessageEnum;
import com.briup.exception.SystemException;
import com.briup.mapper.FenceHouseMapper;
import com.briup.mapper.HurdlesMapper;
import com.briup.pojo.FenceHouse;
import com.briup.pojo.Hurdles;
import com.briup.result.PageTemplate;
import com.briup.result.Result;
import com.briup.service.FenceHouseService;
import com.briup.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author Lenovo
 * @description 针对表【manager_fence_house】的数据库操作Service实现
 * @createDate 2024-03-13 14:29:39
 */
@Service
public class FenceHouseServiceImpl extends ServiceImpl<FenceHouseMapper, FenceHouse>
        implements FenceHouseService {

    @Autowired
    private FenceHouseMapper fenceHouseMapper;
    @Autowired
    private HurdlesMapper hurdlesMapper;

    @Override
    public PageTemplate fhquery(Integer pageNum, Integer pageSize, String fhName) throws SystemException {
        //参数校验
        if (Objects.isNull(pageNum) || Objects.isNull(pageSize)) {
            //页码或页面大小为空
            throw new SystemException(CodeAndMessageEnum.PAGE_NUMBER_OR_PAGE_SIZE_IS_NULL);
        }
        Page<FenceHouse> fenceHousePage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<FenceHouse> fenceHouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fenceHouseLambdaQueryWrapper.like(Objects.nonNull(fhName) && StringUtils.hasText(fhName), FenceHouse::getFhName, fhName);
        fenceHouseMapper.selectPage(fenceHousePage, fenceHouseLambdaQueryWrapper);
        PageTemplate pageTemplate = new PageTemplate(fenceHousePage.getTotal(), fenceHousePage.getRecords());

        return pageTemplate;
    }

    @Override
    public Result selectById(String fhId) {
        //根据栏舍ID查询栏舍信息
        FenceHouse fenceHouse = fenceHouseMapper.selectById(fhId);
        //创建条件选择器
        LambdaQueryWrapper<Hurdles> hurdlesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据栏舍ID匹配栏圈信息（精确匹配）
        hurdlesLambdaQueryWrapper.eq(Hurdles::getHFenceId, fhId);
        //查询符合条件的栏圈信息集合
        List<Hurdles> hurdles = hurdlesMapper.selectList(hurdlesLambdaQueryWrapper);
        //bean拷贝
        FenceHouseSelectByIdVO fenceHouseSelectByIdVO = BeanCopyUtils.copyBean(fenceHouse, FenceHouseSelectByIdVO.class);
        //将栏圈集合信息封装在vo对象中
        fenceHouseSelectByIdVO.setMhs(hurdles);
        //返回统一响应
        return Result.success(fenceHouseSelectByIdVO);
    }

    @Override
    public Result fenceSaveOrUpdate(AddOrUpdateFenceHouseDTO addOrUpdateFenceHouseDTO) throws SystemException {
        //参数校验
        if (Objects.isNull(addOrUpdateFenceHouseDTO.getFhName()) || !StringUtils.hasText(addOrUpdateFenceHouseDTO.getFhName())) {
            throw new SystemException(CodeAndMessageEnum.FENCE_HOUSE_NAME_IS_NULL);
        }
        //bean拷贝
        FenceHouse fenceHouse = BeanCopyUtils.copyBean(addOrUpdateFenceHouseDTO, FenceHouse.class);
        /*
         * 根据请求体中是否包含栏舍ID来判断
         */
        if (Objects.isNull(addOrUpdateFenceHouseDTO.getFhId())) {
            /*
             * 请求体中不含有栏舍ID  新增操作
             */
            fenceHouseMapper.insert(fenceHouse);
            return Result.success();
        } else {
            /*
             * 请求体中含有栏舍ID 修改操作
             */
            fenceHouseMapper.updateById(fenceHouse);
            return Result.success();
        }
    }

    @Override
    public Result deleteById(String hid) throws SystemException {
//        参数校验
        FenceHouse fenceHouse = fenceHouseMapper.selectById(hid);
        if (Objects.isNull(fenceHouse)) {
            throw new SystemException(CodeAndMessageEnum.FENCE_HOUSE_IS_NULL);
        }
        fenceHouseMapper.deleteById(hid);
        return Result.success();
    }

    @Override
    public Result deleteByIdAll(List<String> hids) throws SystemException {
        LambdaQueryWrapper<FenceHouse> fenceHouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fenceHouseLambdaQueryWrapper.in(Objects.nonNull(hids) && !hids.isEmpty()
                , FenceHouse::getFhId, hids);
        List<FenceHouse> fenceHouses = fenceHouseMapper.selectList(fenceHouseLambdaQueryWrapper);
        if (hids.size() != fenceHouses.size()){
//            有id找不到对应的信息
            throw new SystemException(CodeAndMessageEnum.FENCE_HOUSE_IS_NULL);
        }
        fenceHouseMapper.deleteBatchIds(hids);
        return Result.success();
    }
}




